package com.wzx.user.controller;

import com.wzx.common.exception.LeadNewsException;
import com.wzx.common.pojo.resp.Result;
import com.wzx.user.dto.*;
import com.wzx.user.mongo.GroupInfoDto;
import com.wzx.user.mongo.SendMsg;
import com.wzx.user.mongo.SystemMsg;
import com.wzx.user.pojo.Group;
import com.wzx.user.service.GroupService;
import com.wzx.user.vo.GroupAddAdmin;
import com.wzx.user.vo.GroupQueryUserVo;
import com.wzx.user.vo.GroupVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wzx
 * @version 1.0
 * @date 2022/7/28 - 21:34
 * @description 标题
 */
@Slf4j
@RestController
@RequestMapping("/group")
@Api(tags = "群组管理管理")
public class GroupController {

    @Autowired
    private GroupService groupService;

    @ApiOperation(value = "创建群聊")
    @PostMapping("/createGroup")
    public Result<GroupDto> createGroup(@RequestBody GroupVo groupVo) throws LeadNewsException {
        GroupDto groupDto = groupService.createGroup(groupVo);
        return Result.ok(groupDto);
    }

    @ApiOperation(value = "获取群聊消息")
    @GetMapping("/getMsgByGroup")
    public Result getMsgByGroup(Integer page, Integer size, Integer gid) {
        return groupService.getMsgByGroup(page, size, gid);
    }

    @ApiOperation(value = "获取群聊信息")
    @GetMapping("/getGroupInfo")
    public Result<GroupInfoDto> getGroupInfo(Integer gid) {
        GroupInfoDto groupInfoDto = groupService.getGroupInfo(gid);
        return Result.ok(groupInfoDto);
    }

    @ApiOperation(value = "加入群聊")
    @PostMapping("/addGroup")
    public Result<SendMsg> addGroup(@RequestBody SystemMsg systemMsg) {
        SendMsg sendMsg = groupService.addGroup(systemMsg);
        return Result.ok(sendMsg);
    }

    @ApiOperation(value = "查询可拉入群聊的好友")
    @GetMapping("/getGroupFriend")
    public Result getGroupFriend(Integer userId, Integer gid) {
        List<UserDto> groupFriend = groupService.getGroupFriend(userId, gid);
        return Result.ok(groupFriend);
    }

    @ApiOperation(value = "删除群聊")
    @GetMapping("/deleteGroupById")
    public Result<String> deleteGroupById(Integer groupId, Integer userId) {
        groupService.deleteGroupById(groupId, userId);
        return Result.ok();
    }

    @ApiOperation(value = "批量删除群聊")
    @PostMapping("/deleteGroupByIds")
    public Result<String> deleteGroupByIds(@RequestBody GroupVo groupVo) {
        //System.err.println("groupId = " + groupVo);
        String[] split = groupVo.getUserIds().split(",");
        Integer[] intTemp = new Integer[split.length];
        for (int i = 0; i < split.length; i++) {
            intTemp[i] = Integer.parseInt(split[i]);
        }

        groupService.deleteGroupByIds(groupVo.getGroupId(), intTemp);
        return Result.ok();
    }

    @ApiOperation(value = "查询群聊的昵称，头像，备注")
    @GetMapping("/queryGroupByHeadInfo")
    public Result<GroupHeadInfoDto> queryGroupByHeadInfo(Integer groupId, Integer userId) throws LeadNewsException {
        GroupHeadInfoDto groupHeadInfoDto = groupService.queryGroupByHeadInfo(groupId, userId);
        return Result.ok(groupHeadInfoDto);
    }

    @ApiOperation(value = "修改群聊的昵称，头像，备注")
    @PostMapping("/updateGroupByHeadInfo")
    public Result updateGroupByHeadInfo(@RequestBody GroupHeadInfoDto groupHeadInfoDto) throws LeadNewsException {
        groupService.updateGroupByHeadInfo(groupHeadInfoDto);
        return Result.ok();
    }

    @ApiOperation(value = "查询群聊的成员")
    @PostMapping("/queryGroupUsers")
    public Result<List<GroupUserDto>> queryGroupUsers(@RequestBody GroupQueryUserVo groupQueryUserVo) throws LeadNewsException {
        List<GroupUserDto> groupUserDtoList = groupService.queryGroupUsers(groupQueryUserVo);
        return Result.ok(groupUserDtoList);
    }

    @ApiOperation(value = "根据群聊id查询群聊的基本信息")
    @GetMapping("/queryGroupInfo")
    public Result<Group> queryGroupInfo(Integer groupId) {
        Group group = groupService.queryGroupInfo(groupId);
        return Result.ok(group);
    }

    @ApiOperation(value = "更改群发言模式")
    @PostMapping("/changeGroupState")
    public Result changeGroupState(@RequestBody GroupInfoStateDto groupInfoStateDto) {
        groupService.changeGroupState(groupInfoStateDto);
        return Result.ok("修改成功！");
    }

    @ApiOperation(value = "更改用户身份")
    @PostMapping("/changeUsersAdmin")
    public Result changeUsersAdmin(@RequestBody GroupAddAdmin groupAddAdmin) {
        groupService.changeUsersAdmin(groupAddAdmin);
        return Result.ok("修改成功！");
    }

    @ApiOperation(value = "解散群聊")
    @GetMapping("/dissolutionGroup")
    public Result dissolutionGroup(Integer groupId) {
        groupService.dissolutionGroup(groupId);
        return Result.ok();
    }

    @ApiOperation(value = "查询用户加入了哪些群聊")
    @GetMapping("/queryGroupByUser")
    public Result queryGroupByUser(Integer userId) {
        List<Group> list = groupService.queryGroupByUser(userId);
        return Result.ok(list);
    }


}
